package com.motion.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.motion.generator.entity.CourseClass;
import com.motion.generator.entity.CourseLink;
import com.motion.generator.entity.UserCourse;
import com.motion.generator.mapper.CourseClassMapper;
import com.motion.generator.mapper.CourseLinkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CourseLinkService {
    @Autowired(required = false)
    private CourseLinkMapper courseLinkMapper;
    @Autowired(required = false)
    private CourseClassMapper courseClassMapper;

    public List<String> getCourseLabel(Long courseId){
        QueryWrapper<CourseLink> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("courseId", courseId);
        List<CourseLink> courseLinks=courseLinkMapper.selectList(queryWrapper);
        List<String> stringList=new ArrayList<>();
        for (CourseLink item:courseLinks){
            stringList.add(courseClassMapper.selectById(item.getCourseClassId()).getClassValue());
        }
        return stringList;
    }

    public List<Long> getCourseClassId(Long courseId){
        QueryWrapper<CourseLink> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("courseId",courseId);
        List<CourseLink> courseLinks=courseLinkMapper.selectList(queryWrapper);
        return courseLinks.stream().map(CourseLink::getCourseClassId).collect(Collectors.toList());
    }

    public List<CourseLink> selectCourseLink(List<List<Long>> lists){
        QueryWrapper<CourseLink> queryWrapper=new QueryWrapper<>();
        List<Long> longList=courseLinkMapper.selectList(queryWrapper).stream().map(CourseLink::getCourseId).collect(Collectors.toList());
        for (List<Long> item:lists){
            queryWrapper=new QueryWrapper<>();
            queryWrapper.in("courseId",longList);
            if(longList.size()==0){
                return new ArrayList<>();
            }
            queryWrapper.in("courseClassId", item);
            List<CourseLink> courses=courseLinkMapper.selectList(queryWrapper);
            longList=courses.stream().map(CourseLink::getCourseId).collect(Collectors.toList());
        }
        List<CourseLink> courses=courseLinkMapper.selectList(queryWrapper);
        return courseLinkMapper.selectList(queryWrapper);
    }
    public CourseLink insertCourse(Long courseId,Long courseClassId){
        CourseLink courseLink=new CourseLink();
        courseLink.setCourseClassId(courseClassId);
        courseLink.setCourseId(courseId);
        courseLinkMapper.insert(courseLink);
        return courseLink;
    }
    public int delectCourseLink(Long courseLinkId){
        return courseLinkMapper.deleteById(courseLinkId);
    }
    public List<CourseLink> getCourseLinkBycourse(Long courseId){
        QueryWrapper<CourseLink> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("courseId",courseId);
        return courseLinkMapper.selectList(queryWrapper);
    }

}
